//************************************************************
//
// DATASTEP 1. BUILDING ASSIGNEE PATENT DATABASE
//
// DATASOURCE: USPTO
//
// AUTHOR: LIONEL NESTA
//
//*************************************************************

// PREAMBLE

clear
clear matrix 
clear mata
set more off
set matsize 800
cd "USPTO_SOURCE"

// WORKING ON SOURCE FILE FOR PARI CONSOLIDATION 1998

insheet using consolidation.txt, tab names clear
save lmc_assignee.dta, replace

use  parid_gvkey.dta, clear
rename parid parent
mmerge  parent using lmc_assignee.dta, type(1:n)
drop  parent_name affiliate_name _merge
replace affiliate = parent if affiliate == .
save parid_gvkey_consolidation.dta, replace

keep gvkey
duplicates drop
save sample_rd.dta, replace

// WORKING ON ASSIGNEE PAT

use ass_pat_76_06.dta, clear
append using ass_pat_63_99
duplicates drop
compress

mmerge patnum using pat_year_all.dta, type(n:1) unmatched(master)
drop _merge
rename appyear year

// KEEPING ONLY RELEVANT INFO : DROPS DUPLICATES

rename parent parid
keep   gvkey year patnum 
order   gvkey year patnum 
duplicates drop

// AND SAVING

save firm_pat.dta, replace

collapse (count) patent = patnum, by(gvkey year)
drop if year<1963
drop if gvkey==.
drop if year==.

tsset gvkey year
tssmooth ma patsmooth =  patent, window(2 1)

save firm_patprod.dta, replace


